/**
 * 登录页面
 */
let log = new Vue({
    el: '#login',
    data: {

    },
    methods: {
        // 登录
        login: function () {
            let formData = {
                name: $("#name").val(),
                pass: $("#pass").val(),
                code: $("#code").val()
            }

            $.ajax({
                url: 'http://localhost:8080/cinema/api/LoginServlet.do',
                type: 'get',
                crossDomain: true,
                xhrFields: {
                    withCredentials: true
                },
                data: formData,
                dataType: 'json',
                success: function (dto) {
                    if (dto.code == 200) {
                        if (dto.value.level == '管理员' || dto.value.level == '超级管理员') {
                            // 设置会话存储数据
                            let admin = {
                                adminId: dto.value.adminId,
                                adminName: dto.value.adminName,
                                password: dto.value.password,
                                level: dto.value.level,
                                image: dto.value.image,
                                registerTime: dto.value.registerTime,
                                status: dto.value.status
                            }
                            // 如果值是对象，则通过“JSON.stringify"转换
                            sessionStorage.setItem('admin', JSON.stringify(admin));
                            // 跳转页面
                            location.href = '../manage/manage_index.html';
                        } else {
                            if(dto.value.status!=1){
                                layer.msg("您的账号存在异常，已被限制登录！",{
                                    offset:'t',
                                    icon:2,
                                    anim:5
                                });
                                return;
                            }
                            // 设置会话存储数据
                            let user = {
                                userId: dto.value.userId,
                                userName: dto.value.userName,
                                password: dto.value.password,
                                phone: dto.value.phone,
                                sex: dto.value.sex,
                                image: dto.value.image,
                                registerTime: dto.value.registerTime,
                                status: dto.value.status

                            }
                            sessionStorage.setItem('user', JSON.stringify(user));

                            // 跳转页面
                            location.href = '../home/home_index.html';
                        }

                    } else {
                        layer.msg(dto.msg,{
                            offset:'t',
                            icon:5,
                            anim:5
                        });
                    }
                }
            });

        },
        // 验证账号是否存在
        checkName: function () {
            let name = $("#register_name");
            if (name.val() == "") {
                return;
            }

            let formData = {
                userName: name.val()
            }
            $.get('http://localhost:8080/cinema/api/UserCheckServlet.do', formData, function (dto) {
                if (dto.value != null) {
                    layer.msg("账号已存在！",{
                        offset:'t',
                        icon:5,
                        anim:6
                    });
                    name.focus();
                }
            }, "json");

        },
        // 注册操作
        register: function () {
            let name = $("#register_name");
            var phone = $("#phone");
            var respass = $("#register_password");
            var crmpass = $("#confirmPassword");
            var reg = /^1[0-9]{10}$/;

            if (phone.val() == "" || respass.val() == "" || crmpass.val() == "" || name.val() == "") {
                layer.msg("信息需填写完整方可注册！",{
                    offset:'t',
                    icon:5,
                    anim:6
                });
                return;
            }
            if (!reg.test(phone.val())) {
                layer.msg("手机号必须要以1开头的十一位数字组成！",{
                    offset:'t',
                    icon:5,
                    anim:6
                });
                phone.focus();
                return;
            }

            if (respass.val() != crmpass.val()) {
                layer.msg('两次输入新密码需一致！',{
                    offset:'t',
                    icon:5,
                    anim:6
                });
                return;
            }

            let formData = {
                username: name.val(),
                password: respass.val(),
                phone: phone.val()
            }

            $.post('http://localhost:8080/cinema/api/UserRegisterServlet.do', formData, function (dto) {
                if (dto.code == 200) {
                    layer.msg(dto.value,{
                        offset:'t',
                        icon:6,
                        anim:5
                    });
                    // 清空文本框
                    name.val('');
                    respass.val('');
                    crmpass.val('');
                    phone.val('');

                } else {
                    layer.msg(dto.msg);
                }
            }, "json");

        },
        // 实现回车登录
        keyDown(e) {
            if (e.keyCode == 13) {
                this.login();
            }
        }

    },
    mounted() {
        // 绑定监听事件
        window.addEventListener("keydown", this.keyDown);
    },
    destroyed() {
        // 销毁事件
        window.removeEventListener("keydown", this.keyDown, false);
    }


});

